import Vue from "vue";
/* 主要的组件 */
import App from "./App.vue";
/* 路由实例 */
import router from "./router";
/* 状态存储区 */
import store from "./store";
/* ui组件库 */
import elementUi from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
/* 初始化css样式 */
import "./assets/css/minireset.css";
/* 全局样式 */
import "./assets/css/commen.css";
/* icon图标 */
import "./assets/iconfont/index.css";
/* 生成菜单组件 */
import qfSubMenu from "qf-sub-menu";
import save from "utils/save";

/* 全局指令 */
Vue.directive('competence', {
  bind(el, config) {
    const btns = save.takeOut('BUTTONS') || []
    const isIn = btns.indexOf(config.value) < 0
    if (isIn) {
      el.classList.add('is-disabled')
      el.disabled = true
    }

  }
})

Vue.prototype.$save = save;

router.beforeEach(async function (to, from, next) {
  if (!new RegExp("login").test(to.path)) {
    if (!save.takeOut("TOKKU")) {
      next({ name: "login" });
      return;
    }
  }
  const { sides } = store.state;
  if (sides && sides.length === 0) {
    if (!new RegExp("login").test(to.path)) {
      store.dispatch("DIS_SIDE");
    }
  }
  next();
});

router.afterEach((to, form) => {
  const matched = to.matched.slice(1).map(route => ({ toName: route.name, name: route.meta.name }))
  store.commit('RAISE_BRAND', matched)
})

Vue.config.productionTip = false;

Vue.use(elementUi);
Vue.use(qfSubMenu);

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
